import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ball_lion/base/base_controller.dart';
import 'package:ball_lion/server/api/expert_api.dart';
import 'package:ball_lion/server/api/other_api.dart';
import 'package:ball_lion/utils/user.dart';
import 'package:ball_lion/utils/utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:ball_lion/entity/expert/plan_form/plan_form_entity.dart';
import 'package:ball_lion/entity/other/resource_entity.dart';
import 'package:ball_lion/widgets/bottom_picker_widget.dart';

class PlanFormLogic extends BaseController with GetTickerProviderStateMixin {
  List<String> tabs = ["已购", "关注", "连红", "回报"];
  late List<List<String>> stages = List.generate(
      tabs.length, (index) => ["全部阶段", "阶段一", "阶段二", "阶段三", "阶段四"]);
  @override
  EasyRefreshController refreshController = EasyRefreshController();
  ScrollController refreshScroll = ApmScrollController();
  late TabController tabController =
      TabController(length: tabs.length, vsync: this, initialIndex: _tabIndex);
  late List<List<PlanForm>?> items =
      List.generate(tabs.length, (index) => null);
  int _tabIndex = 2;
  late List<int> stageIndex = List.generate(tabs.length, (index) => 0);
  List<ResourceEntity>? lbt;
  bool toTop = false;

  String currentStage(int index) => stages[index][stageIndex[index]];
  int currentStageNum(int index) {
    List<String> list = ["全部阶段", "阶段一", "阶段二", "阶段三", "阶段四", "阶段五", "阶段六"];
    return list.indexOf(currentStage(index));
  }

  int get tabIndex => _tabIndex;

  set tabIndex(int value) {
    _tabIndex = value;
    update();
  }

  @override
  void onInit() {
    tabController.addListener(() {
      tabIndex = tabController.index;
      if (items[tabIndex] == null) getData();
    });
    refreshScroll.addListener(() {
      if (refreshScroll.offset >= refreshScroll.position.maxScrollExtent &&
          toTop == false) {
        toTop = true;
        update();
      }
      if (refreshScroll.offset < refreshScroll.position.maxScrollExtent &&
          toTop == true) {
        toTop = false;
        update();
      }
    });
    super.onInit();
  }

  @override
  void onReady() {
    getData();
    getResource();
    super.onReady();
  }

  Future getData() async {
    if ((tabIndex == 0 || tabIndex == 1) && !User.isLogin) {
      items[tabIndex] = [];
      update();
      return;
    }
    items[tabIndex] = await ExpertApi.getPlanForm(null, tabIndex + 1);
    int stage1 = 0;
    int stage2 = 0;
    int stage3 = 0;
    int stage4 = 0;
    items[tabIndex]?.forEach((element) {
      switch (element.stage) {
        case 1:
          stage1++;
          break;
        case 2:
          stage2++;
          break;
        case 3:
          stage3++;
          break;
        case 4:
          stage4++;
          break;
      }
    });
    stages[tabIndex] = [
      "全部阶段",
      if (stage1 > 0) "阶段一",
      if (stage2 > 0) "阶段二",
      if (stage3 > 0) "阶段三",
      if (stage4 > 0) "阶段四"
    ];
    if (stageIndex[tabIndex] > stages[tabIndex].length - 1) {
      stageIndex[tabIndex] = 0;
    }
    update();
  }

  Future getResource() async {
    final result = await OtherApi.getAppList("app_jhd");
    lbt = result?.where((element) => element.button == "lbt").toList();
    update();
  }

  @override
  Future getRefresh() async {
    getData();
    getResource();
  }

  Future showDatePicker() async {
    stageIndex[tabIndex] = await Get.bottomSheet(BottomPickerWidget(
      stages[tabIndex],
      title: '选择阶段',
      initialIndex: stageIndex[tabIndex],
    ));
    update();
  }

  Future doRefresh() async {
    if (refreshController.headerState?.mode.isIdle == false) return;
    refreshController.callRefresh(scrollController: refreshScroll);
  }
}
